package com.shuai.jit.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuai.jit.entity.chat.Friend;
import com.shuai.jit.entity.user.User;
import com.shuai.jit.entity.vo.VoFriend;
import com.shuai.jit.entity.vo.VoUser;
import com.shuai.jit.mapper.FriendMapper;
import com.shuai.jit.mapper.UserMapper;
import com.shuai.jit.service.FriendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@Service
@Transactional
public class FriendServiceImpl extends ServiceImpl<FriendMapper, Friend> implements FriendService {


    UserMapper userMapper;

    public FriendServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }


    /**
     * 获取好友列表信息
     * @param userId 用户id
     * @return
     */
    @Override
    public List<VoFriend> getFriend(String userId) {

        List<VoFriend> voFriends = new ArrayList<>();
        List<Friend> list = this.baseMapper.getFriend(userId);

        for (Friend friend : list) {
            User user = userMapper.selectById(friend.getFriendId());
            if(user!=null){
                VoFriend voFriend = new VoFriend();
                voFriend.setFriendId(user.getId());
                voFriend.setFriendName(user.getUsername());
                voFriend.setAvatar(user.getUserImg());
                voFriends.add(voFriend);
            }
        }
        return voFriends;
    }

    /**
     * 获取可以添加好友的用户信息
     * @param userId
     * @return
     */
    @Override
    public List<VoUser> addAllFriend(String userId) {
        List<User> users = userMapper.selectAddAllFriend(userId);
        List<VoUser> voFriends = new ArrayList<>();
        for (User user : users) {
            VoUser voFriend = new VoUser();

            // 排除自己，以防自己被添加在上面
            if(user.getId().equals(userId)){
                continue;
            }
            voFriend.setId(user.getId());
            voFriend.setUserImg(user.getUserImg());
            voFriend.setUsername(user.getUsername());
            voFriends.add(voFriend);
        }
        return voFriends;
    }

    /**
     * 添加好友信息
     * @param userId  用户id
     * @param friendId  好友id
     * @return
     */
    @Override
    public String addFriend(String userId, String friendId) {

        // 首先检查，是否已经添加好友信息
        QueryWrapper<Friend> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("friend_id",friendId);
        Friend temp=this.baseMapper.selectOne(queryWrapper);

        if(temp==null){
            Friend friend = new Friend();
            friend.setFriendId(friendId);
            friend.setUserId(userId);
            Friend two = new Friend();
            two.setFriendId(userId);
            two.setUserId(friendId);
            int result = this.baseMapper.insert(friend);
            result+= this.baseMapper.insert(two);
            if (result > 0) {
                return "添加好友成功";
            } else {
                return "添加好友失败";
            }
        }else {
            return "两个人已经是好友了";
        }

    }

    @Override
    public String deleteFriend(String userId, String friendId) {
        Friend friend = this.baseMapper.getID(userId, friendId);
        if (friend != null) {
            this.baseMapper.deleteById(friend.getId());
            return "删除成功";
        }
        return "删除好友失败";
    }
}
